package com.supreme.stockserver.service;

import com.alibaba.fastjson.JSONObject;
import com.supreme.stockserver.service.remote.FeignApiService;
import com.supreme.common.entity.CreateOrder;
import com.supreme.common.dto.UpdateGoodsStockDTO;
import com.supreme.common.entityVO.CreateOrderVO;
import com.supreme.commonboot.exception.BizException;
import com.supreme.commonboot.exception.TsaException;
import com.supreme.commonmybatis.entity.primary.MBGoods;
import com.supreme.commonentity.mysql.primary.BizOrder;
import com.supreme.commonmybatis.entity.primary.MBStorage;
import com.supreme.commonentity.mysql.primary.SysUser;
import com.supreme.commonmybatis.mapper.GoodsMapper;
import com.supreme.commonmybatis.mapper.UserMapper;
import com.supreme.commonmybatis.mapper.OrderMapper;
import com.supreme.commonmybatis.mapper.StorageMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class OrderService {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private UserMapper userMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StorageMapper storageMapper;

    @Resource
    private FeignApiService feignApiService;

    public int testZero(int status) {
        return status;
    }

    @GlobalTransactional
    public JSONObject processStorage(MBStorage storage) {
        int insert = this.storageMapper.insert(storage);
        ResponseEntity<JSONObject> forEntity = this.restTemplate.postForEntity("http://supreme-simple-server/api/seata/test", storage, JSONObject.class);
        return forEntity.getBody();
    }

    public BizOrder createOrder(Long userId, Long goodsId, Integer goodsNum) {
        final String orderCode = RandomStringUtils.randomNumeric(10);
        final BizOrder order = new BizOrder()
                .setOrderCode(orderCode)
                .setUserId(userId)
                .setGoodsId(goodsId)
                .setOrderType(1)
                .setOrderNum(goodsNum);
        this.orderMapper.insert(order);
        return order;
    }

    @Transactional(rollbackFor = Exception.class)
    public CreateOrder processSaleOrder(Long userId, Long goodsId, Integer goodsNum) {
        final BizOrder order = createOrder(userId, goodsId, goodsNum);

        if (order.getOrderNum() > 100) {
            throw new TsaException("库存数量不能大于100");
        }

        return new CreateOrder().setOrderCode(order.getOrderCode());
    }

    @GlobalTransactional
    @Transactional(rollbackFor = BizException.class)
    public void createOrder(CreateOrderVO createOrderVO) {
        final Long userId = createOrderVO.getUserId();
        final Long goodsId = createOrderVO.getGoodsId();
        final Integer buyNum = createOrderVO.getBuyNum();

        final SysUser user = userMapper.selectById(userId);
        if (Objects.isNull(user)) {
            throw new BizException("user not found");
        }

        final MBGoods goods = goodsMapper.selectById(goodsId);
        if (Objects.isNull(goods)) {
            throw new BizException("goods not found");
        }

        final BizOrder order = new BizOrder();
        order.setOrderCode(String.valueOf(System.currentTimeMillis()));
        order.setOrderNum(buyNum);
        order.setOrderType(1);
        order.setUserId(userId);
        order.setGoodsId(goodsId);
        orderMapper.insert(order);

        final UpdateGoodsStockDTO updateGoodsStockDTO = new UpdateGoodsStockDTO();
        updateGoodsStockDTO.setGoodsId(goodsId);
        updateGoodsStockDTO.setGoodsNum(buyNum);
        feignApiService.updateGoodsStock(updateGoodsStockDTO);
    }
}
